package com.rightsidetech.activity.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.activity.dto.request.ride.*;
import com.rightsidetech.activity.dto.response.ActRideCountResp;
import com.rightsidetech.activity.dto.response.ActRideDetailResp;
import com.rightsidetech.activity.dto.response.AgentRideResp;
import com.rightsidetech.activity.dto.response.RideActivityResp;
import com.rightsidetech.activity.entity.ActRideActivity;
import com.rightsidetech.activity.mapper.ActRideActivityMapper;
import com.rightsidetech.activity.service.ActRideService;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.commom.utils.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("actRideService")
public class ActRideServiceImpl implements ActRideService {

    @Resource
    private ActRideActivityMapper actRideActivityMapper;


    @Override
    public ResponseData applyRide(ApplyRideReq req) {
//        if (actRideActivityMapper.selectMoonCount(req.getAgentInfoId()) >= 3) {
//            return ResponseData.fail("一个月只允许申请三次骑行活动");
//        }
        ActRideActivity record = new ActRideActivity();
        record.setAreaId(req.getAreaId());
        record.setAreaName(req.getAreaName());
        record.setAgentInfoId(req.getAgentInfoId());
        record.setAgentName(req.getAgentName());
        record.setAgentLoginId(req.getAgentLoginId());
        record.setAgentLoginName(req.getAgentLoginName());
        record.setStoreName(req.getStoreName());
        record.setStoreId(req.getStoreId());
        record.setModel(req.getModel());
        record.setStatus((byte)0);
        record.setCreatedAt(new Date());
        record.setStartTime(req.getStartTime());
        String endTime = DateUtil.getDate(req.getEndTime(), "yyyy-MM-dd") + " 23:59:59";
        record.setEndTime(DateUtil.getDate(endTime,"yyyy-MM-dd HH:mm:ss"));
        if (actRideActivityMapper.insertSelective(record) <= 0) {
            return ResponseData.fail("申请骑行活动失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public ResponseData<PageInfo<AgentRideResp>> agentPage(PageRequest req,Long agentInfoId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<AgentRideResp> result = actRideActivityMapper.selectAgentPage(agentInfoId);
        for (AgentRideResp resp : result) {
            if(StrUtil.isNotBlank(resp.getPictures())){
                resp.setPicture(Arrays.asList(resp.getPictures().split(",")));
            }
        }
        return ResponseData.success(new PageInfo<>(result));
    }

    @Override
    public ResponseData<PageInfo<RideActivityResp>> page(ActRidePageReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<RideActivityResp> result = actRideActivityMapper.selectPageList(req);
        for (RideActivityResp resp : result) {
            if(StrUtil.isNotBlank(resp.getPictures())){
                resp.setPicture(Arrays.asList(resp.getPictures().split(",")));
            }
        }
        return ResponseData.success(new PageInfo<>(result));
    }

    @Override
    public ResponseData byRideAct(ByRideActReq req,String verifyName,Long verifyId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        if(req.getStatus() == 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"是否通过状态不正确");
        }
        ActRideActivity rideActivity = actRideActivityMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(rideActivity)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "未找到需要操作的活动");
        }
        if(rideActivity.getStatus() != 0){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "活动状态不正确");
        }
        if(req.getStatus() == -1){
            if(StrUtil.isBlank(req.getRemark())){
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"拒绝活动备注不可为空");
            }
        }
        rideActivity = new ActRideActivity();
        rideActivity.setId(req.getId());
        rideActivity.setStatus(req.getStatus());
        rideActivity.setRemark(req.getRemark());
        rideActivity.setVerifyId(verifyId);
        rideActivity.setVerifyName(verifyName);
        rideActivity.setVerifyDate(new Date());
        if (actRideActivityMapper.updateByPrimaryKeySelective(rideActivity) <= 0) {
            return ResponseData.fail("操作失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public ResponseData upPicture(AgentUpPictureReq req,Long agentLoginId,String agentLoginName) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        ActRideActivity rideActivity = actRideActivityMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(rideActivity)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "没有找到骑行活动");
        }
        if(agentLoginId.equals(rideActivity.getAgentInfoId())){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "该活动不属于此代理商");
        }
        if(rideActivity.getStatus() != 1 && rideActivity.getStatus() != -2){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "活动状态不正确");
        }
        String picture = req.getPicture().stream().collect(Collectors.joining(","));
        rideActivity = new ActRideActivity();
        rideActivity.setId(req.getId());
        rideActivity.setPictures(picture);
        rideActivity.setStatus((byte)2);
        rideActivity.setUpTime(new Date());
        rideActivity.setUploadLoginId(agentLoginId);
        rideActivity.setUploadLoginName(agentLoginName);
        if (actRideActivityMapper.updateByPrimaryKeySelective(rideActivity) <= 0) {
            return ResponseData.fail("操作失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public ResponseData verifyRideAct(VerifyRideActReq req, String verifyName, Long verifyId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        ActRideActivity rideActivity = actRideActivityMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(rideActivity)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "为找到需要审核的骑行活动");
        }
        if(rideActivity.getStatus() != 2){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "骑行活动状态不正确");
        }
        ActRideActivity record = new ActRideActivity();
        record.setId(req.getId());
        record.setVerifyName(verifyName);
        record.setVerifyId(verifyId);
        record.setVerifyDate(new Date());
        record.setRemark(req.getRemark());
        if(req.getVerify() == 0){
            if(StrUtil.isBlank(req.getRemark())){
                return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(), "驳回活动备注不允许为空");
            }
            record.setStatus((byte)-2);
            if (actRideActivityMapper.updateByPrimaryKeySelective(record) <= 0) {
                return ResponseData.fail("审核失败");
            }
            return ResponseData.success(CodeIdEnum.Success);
        }
        record.setStatus((byte)3);
        if (actRideActivityMapper.updateByPrimaryKeySelective(record) <= 0) {
            return ResponseData.fail("审核失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public ResponseData againSubmit(AgainSubmitRideReq req,ApplyRideReq actReq) {
        ActRideActivity activity = actRideActivityMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtil.isEmpty(activity)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"没有找到要操作的数据");
        }
        if(activity.getStatus() != -1){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"活动状态不正确");
        }
        if (!activity.getAgentLoginId().equals(actReq.getAgentLoginId())) {
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"该活动不属于此代理商");
        }
        activity = new ActRideActivity();
        activity.setId(req.getId());
        activity.setAgentInfoId(actReq.getAgentInfoId());
        activity.setAgentName(actReq.getAgentName());
        activity.setAgentLoginId(actReq.getAgentLoginId());
        activity.setAgentLoginName(actReq.getAgentLoginName());
        activity.setStoreName(actReq.getStoreName());
        activity.setStoreId(actReq.getStoreId());
        activity.setModel(actReq.getModel());
        activity.setStartTime(req.getStartTime());
        activity.setEndTime(req.getEndTime());
        activity.setStatus((byte)0);
        if (actRideActivityMapper.updateByPrimaryKeySelective(activity) <= 0) {
            return ResponseData.fail("操作失败");
        }
        return ResponseData.success(CodeIdEnum.Success);
    }

    @Override
    public ActRideActivity selectById(Long id) {
        return actRideActivityMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResponseData<ActRideDetailResp> selectDetail(Long id, Long agentInfoId) {
        ActRideActivity activity = actRideActivityMapper.selectByPrimaryKey(id);
        if(ObjectUtil.isEmpty(activity)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到此骑行活动");
        }
        if (!activity.getAgentLoginId().equals(agentInfoId)) {
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"此骑行活动不属于该代理商");
        }
        ActRideDetailResp result = new ActRideDetailResp();
        result.setId(activity.getId());
        result.setStoreName(activity.getStoreName());
        result.setStoreId(activity.getStoreId());
        result.setStartTime(activity.getStartTime());
        result.setEndTime(activity.getEndTime());
        result.setModel(activity.getModel());
        result.setStatus(activity.getStatus());
        result.setCreatedAt(activity.getCreatedAt());
        result.setPictures(StrUtil.isBlank(activity.getPictures()) ? null : Arrays.asList(activity.getPictures().split(",")));
        return ResponseData.success(result);
    }

    @Override
    public ResponseData<PageInfo<ActRideCountResp>> recordCountPage(ActRideCountReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<ActRideCountResp> result = actRideActivityMapper.selectCountPage(req);
        return ResponseData.success(new PageInfo<>(result));
    }
}
